package com.swq.java.code.chapter13.code28;

/**
 *  这个和code28是一个程序，放同一个包下code29就不用了
 */
public class Machine extends ControlledThread {
    private int count;  //共享数据

    public void run() {
        while (true) {
            synchronized (this) {
                count++;
                System.out.println(Thread.currentThread().getName() + ":run " + count + " times");
            }
            if (!checkState()) {
                System.out.println(Thread.currentThread().getName() + ":stop");
                break;
            }
        }
    }

    public synchronized int getCount() {
        return count;
    }

    public synchronized void reset() {
        count = 0;
        System.out.println(Thread.currentThread().getName() + ":reset");
    }

    public static void main(String args[]) {
        Machine machine = new Machine();
        machine.start();
        for (int i = 0; i < 200; i++) {
            if (machine.getCount() > 5) {
                machine.setState(ControlledThread.SUSP);  //让machine线程暂停
                yield();
                machine.reset();
                machine.setState(ControlledThread.RUN);  //让machine线程恢复运行
            }
            yield();
        }
        machine.setState(ControlledThread.STOP);  //让machine线程终止运行
    }
}